<?
	require('base.php'); 
	$loginError = false;
	
	if ($_GET['task'] == 'logout') { 
		User::logout();
		header("Location: " . SITE_WEB_DIRECTORY . LOGIN_PAGE);
		exit;
	}

	if ($_POST['login']) {
		$result = User::login($_POST['username'], $_POST['password']);
		if ($result) {
			if ($_POST['url']) {
				$url = urldecode($_POST['url']);
				header("Location: {$url}");
				exit;
			} else {
				header("Location: index.php");
				exit;
			}
		} else {
			User::logout();
			$loginError = true;
		}
	}
		
	$page_title = 'Login';
	include('layout/header.php');
?>

<div class="content">
	<h1>login</h1>
	<div class="inset">
	<? if ($loginError) { ?>
		<strong class="error">Either your username or password is incorrect.</strong><br/><br/>
	<? } ?>
	<form method="post" action="<?=$PHP_SELF?>">
		<b>Username</b>:<br>
		<input type="text" value="" name="username" size="20">
		<br><br>
		<b>Password</b>:<br>
		<input type="password" value="" name="password" size="20">
		<br><br>
		<? if ($_GET['url']) { ?>
			<input type="hidden" name="url" value="<?=$_GET['url']?>">
		<? } ?>
		<input type="submit" value="login" name="login" class="button">
	</form>
	</div>
</div>

<? include('layout/footer.php'); ?>